home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9209.ARJ / 1009087A < prev    next >
Text File  |  1992-05-11  |  4KB  |  163 lines

  1.  
  2. /*
  3.      madmain.c
  4.  
  5.      This file contains the main calling
  6.      routine for the Madaline (Modified
  7.      Adaptive Linear Element) program.
  8.  
  9.      Dwayne Phillips
  10.      February 1992
  11.  
  12.    Notes:
  13.   
  14.    the inputs array x has N+2 elements
  15.      x[0] is always 1
  16.      then there are 1 to N elements
  17.      then the last element is the target
  18.    the weights array w has N+1 by A elements
  19.      w[0] is the bias
  20.      then there are 1 to N elements
  21.    the nets array nets has A elements
  22.    the outputs array outs has A elements
  23.   
  24. */
  25.  
  26.  
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <malloc.h>
  32.  
  33.  
  34. main(argc, argv)
  35.   int argc;
  36.   char *argv[];
  37. {
  38.    char  choice;
  39.    char  inputs_file[80], weights_file[80];
  40.    FILE  *inputs, *weights;
  41.    int   files_ok      = 1;
  42.    long  **w, *x, *nets, *outs, N, A;
  43.    int i, j;
  44.  
  45.  
  46.    if(argc < 7){
  47.       printf("\n\nUsage: madaline inputs_file weights_file ");
  48.       printf("\n       size_of_vectors #_of_adalines mode choice_type");
  49.       printf("\n   where mode=t (training)");
  50.       printf("\n   where mode=i (input data)");
  51.       printf("\n   where mode=w (working)");
  52.       printf("\n   where choice_type=m (majority vote)");
  53.       printf("\n   where choice_type=o (logical OR)");
  54.       printf("\n   where choice_type=a (logical AND)");
  55.       exit(1);
  56.    }
  57.  
  58.    strcpy(inputs_file, argv[1]);
  59.    strcpy(weights_file, argv[2]);
  60.  
  61.    N = atoi(argv[3]);
  62.    A = atoi(argv[4]);
  63.  
  64.    if(argv[5][0] != 'i'  &&
  65.       argv[5][0] != 'I'  &&
  66.       argv[5][0] != 't'  &&
  67.       argv[5][0] != 'T'  &&
  68.       argv[5][0] != 'w'  &&
  69.       argv[5][0] != 'W'){
  70.         printf("\nERROR - Did not enter a correct mode");
  71.         exit(1);
  72.    }
  73.  
  74.    choice = argv[6][0];
  75.    if(choice != 'o'  &&
  76.       choice != 'O'  &&
  77.       choice != 'A'  &&
  78.       choice != 'a'  &&
  79.       choice != 'm'  &&
  80.       choice != 'M'){
  81.         printf("\nERROR - Did not enter a correct choice type");
  82.         exit(1);
  83.    }
  84.  
  85.    x    = (long  *) malloc((N+2) * sizeof(long ));
  86.    outs = (long  *) malloc(A * sizeof(long ));
  87.    nets = (long  *) malloc(A * sizeof(long ));
  88.  
  89.    w = malloc(A * sizeof(long  *));
  90.    for(i=0; i<A; i++){
  91.       w[i] = malloc((N+1) * sizeof(long ));
  92.       printf("\n\tw[%d] = %x", i, w[i]);
  93.       if(w[i] == '\0'){
  94.          printf("\n\tmalloc of w[%d] failed", i);
  95.          exit(0);
  96.       }
  97.    }
  98.  
  99.  
  100.  
  101.       /* I N P U T   M O D E */
  102.  
  103.      if(argv[5][0] == 'i'  ||  argv[5][0] == 'I'){
  104.       if( (inputs = fopen(inputs_file, "w+b")) == '\0'){
  105.          printf("\n\nERROR - cannot open input vector file\n");
  106.          exit(0);
  107.       }
  108.       else
  109.          get_straight_input_vectors(inputs, x, N);
  110.    }  /* ends if input_mode */
  111.  
  112.  
  113.       /*  T R A I N I N G   M O D E */
  114.  
  115.    if(argv[5][0] == 't'  ||  argv[5][0] == 'T'){
  116.       if( (inputs = fopen(inputs_file, "r+b")) == '\0'){
  117.          printf("\n\nERROR - cannot open input vector file\n");
  118.          files_ok = 0;
  119.          exit(0);
  120.       }
  121.  
  122.       if( (weights = fopen(weights_file, "w+b")) == '\0'){
  123.          printf("\n\nERROR - cannot open weights vector file\n");
  124.          files_ok = 0;
  125.          exit(0);
  126.       }
  127.  
  128.       if(files_ok){
  129.                 printf("files ok");
  130.          train_the_madaline(inputs, weights, x, w,
  131.                             nets, outs, N, A, choice);
  132.         }
  133.  
  134.     }  /* ends training mode */
  135.  
  136.  
  137.  
  138.       /* W O R K I N G   M O D E */
  139.    if(argv[5][0] == 'w'  ||  argv[5][0] == 'W'){
  140.  
  141.       if( (weights = fopen(weights_file, "r+b")) == '\0'){
  142.          printf("\n\nERROR - cannot open weights vector file\n");
  143.          exit(0);
  144.       }
  145.       else
  146.          process_new_madaline(weights, x, w, nets, outs, choice, N, A);
  147.  
  148.    }  /* ends if working_mode */
  149.  
  150.  
  151.  
  152.       /* free up the allocated space */
  153.  
  154.    for(i=0; i<A; i++)
  155.       free(w[i]);
  156.  
  157.    free(w);
  158.    free(x);
  159.    free(nets);
  160.    free(outs);
  161.  
  162. }  /* ends main */
  163.